package com.gpnu7_412.yanzhi.controller.user;

import com.gpnu7_412.yanzhi.dto.UserMessageDto;
import com.gpnu7_412.yanzhi.entity.UserHanding;
import com.gpnu7_412.yanzhi.exception.custom.RequestDataException;
import com.gpnu7_412.yanzhi.service.UserService;
import com.gpnu7_412.yanzhi.status.Impl.ResultCode;
import com.gpnu7_412.yanzhi.util.Result;
import com.gpnu7_412.yanzhi.util.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.sql.Timestamp;
import java.util.Map;

/**
 * @author ZZH
 * @author HJJ
 */
@CrossOrigin
@RestController
@RequestMapping("/user/update")
public class UpdateController {

    @Autowired
    private UserService userService;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 修改当前登录用户的基本信息
     * @param userMessageDto 请求信息
     * @return 响应对象
     */
    @PutMapping("/base")
    public Result updateBaseMessage(@RequestBody UserMessageDto userMessageDto){
        Map<String, Object> map = ThreadLocalUtil.get();
        Integer userId = (Integer) map.get("userId");
        if (!userId.equals(userMessageDto.getUserId())){
            throw new RequestDataException("权限不足");
        }
        if (!userService.updateBaseMessage(userMessageDto)) {
            throw new RequestDataException("参数为空");
        }
        return new Result();
    }

    /**
     * 修改当前登录用户的头像
     * @param avatarFile 用户头像文件
     * @return 响应对象
     */
    @PutMapping("/avatar")
    public Result updateAvatar(@RequestParam("avatarFile") MultipartFile avatarFile){
        Map<String, Object> map = ThreadLocalUtil.get();
        Integer userId = (Integer) map.get("userId");
        if (!userService.updateAvatar(avatarFile, userId)) {
            throw new RequestDataException("参数为空");
        }
        return new Result();
    }

    /**
     * 修改当前登录用户的密码
     * @param messageMap 请求信息
     * @return 响应对象
     */
    @PutMapping("/password")
    public Result updatePassword(@RequestBody Map<String, Object> messageMap){
        Map<String, Object> threadMap = ThreadLocalUtil.get();
        Integer userId = (Integer) threadMap.get("userId");
        String oldPassword = (String) messageMap.get("oldPassword");
        String newPassword = (String) messageMap.get("newPassword");
        userService.updatePassword(userId, oldPassword, newPassword);
        return new Result(ResultCode.SUCCESS, "密码修改成功");
    }

    /**
     * 修改当前登录用户的邮箱
     * @param messageMap 请求数据
     * @return 响应对象
     */
    @PutMapping("/email")
    public Result updateEmail(@RequestBody Map<String, Object> messageMap){
        String newEmail = messageMap.get("newEmail").toString();
        String verification = messageMap.get("verifyCode").toString();
        ValueOperations<String, String> operations = stringRedisTemplate.opsForValue();
        String storeCode = operations.get(newEmail);
        if (storeCode == null || !storeCode.equals(verification)) {
            return new Result(ResultCode.FAILED, "验证码错误");
        }
        Map<String, Object> stringObjectMap = ThreadLocalUtil.get();
        Integer userId = (Integer) stringObjectMap.get("userId");
        userService.updateEmail(userId, newEmail);
        return new Result(ResultCode.SUCCESS, "邮箱修改成功");
    }

    /**
     * 用户关注
     * @param likeUserId 被关注者ID
     * @return 响应对象
     */
    @PostMapping("/attention")
    public Result attention(@RequestParam("likeUserId") Integer likeUserId){
        Map<String, Object> threadMap = ThreadLocalUtil.get();
        Integer userId = (Integer) threadMap.get("userId");
        if (!userService.attention(userId, likeUserId)) {
            throw new RequestDataException("参数错误");
        }
        return new Result();
    }

    /**
     * 用户取消关注
     * @param likeUserId 被关注者ID
     * @return 响应对象
     */
    @PostMapping("/unfollow")
    public Result unfollow(@RequestParam("likeUserId") Integer likeUserId){
        Map<String, Object> threadMap = ThreadLocalUtil.get();
        Integer userId = (Integer) threadMap.get("userId");
        if (!userService.unfollow(userId, likeUserId)) {
            throw new RequestDataException("参数错误");
        }
        return new Result();
    }

    /**
     * 用户举报
     * @param userHanding 用户举报对象
     * @return 响应对象
     */
    @PostMapping("/report")
    public Result report(@RequestBody UserHanding userHanding) {
        userHanding.setHandingTime(new Timestamp(System.currentTimeMillis()));
        if (!userService.report(userHanding)) {
            throw new RequestDataException("参数为空");
        }
        return new Result();
    }
}
